package sg.edu.nus.iss.jass.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Properties;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;

import sg.edu.nus.iss.jass.model.ResponseDTO;
import sg.edu.nus.iss.jass.util.GUIUtils;

/**
 * @version 1.0
 * @author SE19 Team 1
 * 
 */
public class ViewResponseListPanel extends JPanel {
	private static final long serialVersionUID = 1L;

	public static final String BTN_VIEW_RESPONSE = "btnViewResponse";

	private Properties properties;
	private JPanel headerPanel;
	private JPanel resultPanel;
	private JLabel msgTitle;
	private JLabel lblMessage;
	private JButton btnViewResponse;
	private JTable responsesTable;
	private List<ResponseDTO> responses;

	private DefaultTableModel defaultTableModel = new DefaultTableModel() {
		private static final long serialVersionUID = 1L;

		@Override
		public boolean isCellEditable(int row, int column) {
			return false;
		}
	};

	public ViewResponseListPanel() {
		try {
			this.properties = GUIUtils.loadConfiguration();
			this.initViewResponseScreen();

		} catch (Exception ex) {
			this.msgTitle.setText(ex.getMessage());
		}
	}

	public ViewResponseListPanel(List<ResponseDTO> responses) {
		try {
			this.responses = responses;
			this.displayResultPanel();

		} catch (Exception ex) {
			this.msgTitle.setText(ex.getMessage());
		}
	}

	public void addComponentActionListener(ActionListener listener) {
		this.btnViewResponse.addActionListener(listener);
	}

	private void displayResultPanel() {
		String[] columnNames = { "No", "Candidate Name", "Reference Code", "Job Title", "JobAdvertId" };

		Object[][] data = new Object[this.responses.size()][5];
		for (int n = 0; n < this.responses.size(); n++) {
			ResponseDTO response = this.responses.get(n);

			data[n][0] = String.valueOf(n + 1);
			data[n][1] = response.getCandidateName();
			data[n][2] = response.getJobAdvert().getReferenceCode();
			data[n][3] = response.getJobAdvert().getJobTitle();
			data[n][4] = response;
		}

		this.defaultTableModel.setDataVector(data, columnNames);
		this.responsesTable.setModel(this.defaultTableModel);
		this.responsesTable.removeColumn(this.responsesTable.getColumnModel().getColumn(4));
		this.responsesTable.setPreferredScrollableViewportSize(new Dimension(900, 350));
	}

	private JPanel getButtonPanel() {
		JPanel buttonPanel = new JPanel();
		buttonPanel.setOpaque(false);
		buttonPanel.add(this.btnViewResponse);

		return buttonPanel;
	}

	private JPanel getHeaderPanel() {
		this.headerPanel = new JPanel();
		this.headerPanel.setLayout(new GridLayout(0, 1, 10, 10));
		this.headerPanel.setOpaque(false);

		this.msgTitle = new JLabel(this.properties.getProperty("title.viewResponse.responseList"));
		this.msgTitle.setFont(new Font("Calibri", Font.PLAIN, 30));
		this.msgTitle.setHorizontalAlignment(SwingConstants.CENTER);
		this.headerPanel.add(this.msgTitle);

		ImageIcon imgDivider = new ImageIcon("./images/divider.jpg");
		JLabel lblDivider = new JLabel(imgDivider);
		this.headerPanel.add(lblDivider);

		this.lblMessage = GUIUtils.createLabel("");
		this.lblMessage.setHorizontalAlignment(SwingConstants.CENTER);
		this.headerPanel.add(this.lblMessage);

		return this.headerPanel;
	}

	private JPanel getResultPanel() {
		this.resultPanel = new JPanel();
		this.resultPanel.setOpaque(false);

		this.responsesTable = new JTable();
		this.responsesTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		this.btnViewResponse = new JButton(this.properties.getProperty("button.label.viewResponse"));
		this.btnViewResponse.setName(BTN_VIEW_RESPONSE);
		this.btnViewResponse.setPreferredSize(new Dimension(200, 25));

		this.resultPanel.add(new JScrollPane(this.responsesTable));
		return this.resultPanel;
	}
	
	public String getSelectedResponse() {
		String id = null;
		int selectedRow = this.responsesTable.getSelectedRow();
		
		if (selectedRow > -1) {
			ResponseDTO selectedResponseDTO = (ResponseDTO)this.responsesTable.getModel().getValueAt(selectedRow, 4);
			id = selectedResponseDTO.getId();
			return id;
		}
		else {
			throw new IllegalArgumentException("Please select a response first!");
		}
	}

	private void initViewResponseScreen() {
		this.setLayout(new BorderLayout());
		this.add(this.getHeaderPanel(), BorderLayout.NORTH);
		this.add(this.getResultPanel(), BorderLayout.CENTER);
		this.add(this.getButtonPanel(), BorderLayout.SOUTH);
	}
	
	public void setMessage(String message) {
		this.lblMessage.setText(message);
	}

	public void setResponseList(List<ResponseDTO> responses) {
		this.responses = responses;
		if (responses != null)
			this.displayResultPanel();
	}
}
